//
//
// adventure screen handler for the Mage's Chest Object
//
// L. Dean Gibson II  2/13/01
//

#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "adv_mages_chest.h"

#include "adv_object_model.h"
#include "adventure_frame.h"
#include "adventure_map.h"
#include "adventure_sounds.h"
#include "ai_value.h"
#include "army.h"
#include "artifact.h"
#include "artifact_properties.h"
#include "basic_dialog.h"
#include "bitmap_layer_cache_window.h"
#include "bitmap_layer_cache_window_ptr.h"
#include "default_artifacts.h"
#include "external_string.h"
#include "game_replace_keywords.h"
#include "hero.h"
#include "hero_class_properties.h"
#include "object_registration.h"
#include "mouse_event.h"
#include "music.h"
#include "player.h"
#include "replace_keywords.h"
#include "simple_dialog.h"
#include "town_type.h"

static t_object_registration<t_adv_mages_chest> 
       k_registration( k_adv_object_treasure, k_treasure_mages_chest );

// -------------------------------------------------------
// Mage's Chest object constructor
// -------------------------------------------------------
t_adv_mages_chest::t_adv_mages_chest(  std::string const& source )
			  : t_stationary_adventure_object( source )
{

}

// -------------------------------------------------------
// activate trigger for the Mage's Chest adventure map object
// -------------------------------------------------------
void t_adv_mages_chest::activate_trigger( t_army* army, t_adv_map_point const& point, 
		                                  t_direction direction, t_adventure_frame* frame )
{
	t_player *						  player = army->get_owner();
	t_artifact new_artifact;

	new_artifact = get_default_artifact(m_artifact);

	destroy(); // remove us from the map

	// display some sort of response 
	if (!army->get_owner()->is_computer())
	{
		// give the decided artifact to the hero in the army, if any
		army->add(new_artifact);

		t_basic_dialog*					dialog;
		std::string						text;
		t_bitmap_layer_cache_window_ptr icon_window;
		t_bitmap_layer_ptr				icon = get_icon( m_artifact );

		dialog = new t_basic_dialog;
		text = get_text( "Initial" );
		// add the artifact's name to the displayed text
		text = replace_keywords( text, "%artifact_name", new_artifact.get_name() );
		text = replace_keywords( text, "%an_artifact", new_artifact.get_name( true ));
		dialog->set_text( text );

		dialog->add_artifact( new_artifact );

		dialog->add_ok_button();
		dialog->set_title( get_name() );
		dialog->open( get_dialog_bottom( point ), true, k_align_bottom );
		t_sound_cache music = get_music_playing();
		stop_music();
		get_dialog_sound( k_dialog_sound_treasure )->play( get_sound_volume() );
		dialog->run_modal();
		play_music( music );
	} else
		ai_give_artifact_to_army( *army, new_artifact );
}

// -------------------------------------------------------
// Mage's Chest placement function, decides the treasure contained in the backpack at map load time
// -------------------------------------------------------
void t_adv_mages_chest::initialize( t_adventure_map& map )
{
	// decide what artifact we carry now at place-time, backpack gives item
	m_artifact = map.get_random_artifact(k_artifact_level_minor);
}

// -------------------------------------------------------
// Mage's Chest as it appears on the adventure map
// -------------------------------------------------------
bool t_adv_mages_chest::write( std::streambuf& stream ) const
{
	put<t_artifact_type>( stream, m_artifact );
	return true;
}

// -------------------------------------------------------
// Mage's Chest as it appears on the adventure map
// -------------------------------------------------------
bool t_adv_mages_chest::read( std::streambuf&						stream, 
						      t_qualified_adv_object_type const&	type,
						      int									version )
{
	m_artifact = get<t_artifact_type>( stream );
	return true;
}

void t_adv_mages_chest::right_click( t_mouse_event const& event,
		                            t_adventure_frame* adventure_frame )
{
	t_skill_mastery information = get_information_level();

	if (information < k_mastery_basic)
	{
		t_stationary_adventure_object::right_click( event, adventure_frame );
		return;
	}
	std::string help_text = get_text( "scouting" );

	help_text = replace_keywords( help_text, get_default_artifact( m_artifact ) );
	show_popup_text( help_text, event.screen_point );
}


float t_adv_mages_chest::ai_value( t_adventure_ai const& ai, t_creature_array const& army, int move_cost ) const
{
	return army.get_owner()->ai_get_artifact_value(k_artifact_level_minor);
}


// -------------------------------------------------------
// shipwreck survivor
// -------------------------------------------------------
static t_object_registration<t_adv_shipwreck_survivor> 
       k_survivor_registration( k_adv_object_shipwreck_survivor );

void t_adv_shipwreck_survivor::initialize( t_adventure_map& map )
{
	// decide what artifact we carry now at place-time, backpack gives item
	m_artifact = map.get_random_artifact(k_artifact_level_treasure);
}

int t_adv_shipwreck_survivor::get_version() const
{
	return 1;
}

bool t_adv_shipwreck_survivor::read( std::streambuf&						stream, 
						             t_qualified_adv_object_type const&	type,
						             int									version )
{
	if (version > 0)
		m_artifact = get<t_artifact_type>( stream );
	return true;
}

// -------------------------------------------------------
// Traveler's Backpack placement function, decides the treasure contained in the backpack at map load time
// -------------------------------------------------------
static t_object_registration<t_adv_travelers_backpack> 
       k_backpack_registration( k_adv_object_treasure, k_treasure_travelers_backpack );

void t_adv_travelers_backpack::initialize( t_adventure_map& map )
{
	// decide what artifact we carry now at place-time, backpack gives item
	m_artifact = map.get_random_artifact(k_artifact_level_item);
}

// -------------------------------------------------------
// skeleton 
// -------------------------------------------------------
static t_object_registration<t_adv_skeleton> k_corpse_registration( k_adv_object_corpse );

void t_adv_skeleton::initialize( t_adventure_map& map )
{
	// decide what artifact we carry now at place-time, backpack gives item
	m_artifact = map.get_random_artifact(k_artifact_level_treasure);
}
